package cn.youframe.frame.service;

import cn.youframe.frame.util.SqlBuilder;
import cn.youframe.frame.base.BaseServiceImpl;
import cn.youframe.frame.beans.CommTypeVO;
import cn.youframe.frame.beans.CommTypeValVO;
import cn.youframe.frame.cache.CacheMgr;
import cn.youframe.frame.dao.DictDao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Iterator;

/**
 * User: Administrator
 * DateTime: 13-2-18: 下午4:07
 */
public class DictServiceImpl extends BaseServiceImpl implements DictService {
    public DictDao getDictDao() {
        return dictDao;
    }

    public void setDictDao(DictDao dictDao) {
        this.dictDao = dictDao;
    }

    private DictDao dictDao;
    @Override
    public void initCache() {
        CacheMgr manager= CacheMgr.getInstance();
        try {
            List<CommTypeValVO> list = dictDao.queryDictVals();
            Map<String,SortedMap<String,CommTypeValVO>> valMap=new HashMap<String,SortedMap<String,CommTypeValVO>>();
            // 将每个类型放到Map中
            for(CommTypeValVO valVO:list){
                if(valMap.get(valVO.getTypeId())==null){
                    valMap.put(valVO.getTypeId(), new TreeMap<String, CommTypeValVO>());
                }
                valMap.get(valVO.getTypeId()).put(valVO.getValId(),valVO);
            }
            // 将类型加入缓存
            Iterator<Map.Entry<String,SortedMap<String,CommTypeValVO>>> valIt = null;
            if (valMap != null) {
                valIt = valMap.entrySet().iterator();
                Map.Entry<String,SortedMap<String,CommTypeValVO>> valEty = null;
                while (valIt.hasNext()) {
                    valEty = valIt.next();
                    manager.put(valEty.getKey(), valEty.getValue());
                }
            }
        }catch (Exception ex){
            logger.error(ex.getMessage(),ex);
        }
    }

    @Override
    public List<CommTypeVO> queryTypes() {
        return dictDao.queryTypes();
    }

    @Override
    public int addType(CommTypeVO typeVO) {
        return dictDao.addType(typeVO);
    }

    @Override
    public int doUpdateType(Map<String, String> typeVal) {
        CommTypeVO typeVO=new CommTypeVO();
        String typeId=(String)typeVal.get("typeId");
        typeVO.setTypeId(typeId);
        String typeName=(String)typeVal.get("typeName");
        typeVO.setTypeName(typeName);
        String typeKind=(String)typeVal.get("typeKind");
        typeVO.setTypeKind(typeKind);
        String memo=(String)typeVal.get("memo");
        typeVO.setMemo(memo);
        String oldTypeId=(String)typeVal.get("oldTypeId");
        dictDao.updateType(typeVO,oldTypeId);
        dictDao.updateTypeVals(typeId,oldTypeId);
        return 0;
    }

    @Override
    public int doDelType(String typeId) {
        dictDao.delTypeVals(typeId);
        dictDao.delType(typeId);
        return 0;
    }

    @Override
    public List<CommTypeValVO> getTypeVals(String typeId, String typeKind) {
        List<CommTypeValVO> retLs=null;
        SqlBuilder sqlb=new SqlBuilder();
        sqlb.append("SELECT VSEQ,TYPE_ID,VAL_ID,VAL_VAL,PVAL_ID,VAL_NAME,EXT_VAL,");
        sqlb.append("PRE1,PRE2,PRE3,ENABLE,MEMO FROM YF_COMM_TYPE_VAL WHERE TYPE_ID=?").addArg(typeId);
        if(typeKind.equals("0")){
            retLs=dictDao.queryList(sqlb.getSQL(),CommTypeValVO.class,sqlb.getArgArray());
        }else {

        }
        return retLs;
    }
}
